package cn.smbms.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.smbms.bean.model.FunctionAuthority;
import cn.smbms.bean.model.RoleAuthority;
import cn.smbms.bean.model.SysRole;
import cn.smbms.bean.vo.RoleAuthorityVo;
import cn.smbms.biz.IFunctionAuthorityBiz;
import cn.smbms.biz.IRoleAuthorityBiz;
import cn.smbms.biz.ISysRoleBiz;

@Controller
@RequestMapping("/roleAuthority")
public class RoleAuthorityController {
	@Resource(name = "roleAuthorityBiz")
	private IRoleAuthorityBiz roleAuthorityBiz;
	@Resource(name = "functionAuthorityBiz")
	private IFunctionAuthorityBiz functionAuthorityBiz;
	@Resource(name = "sysRoleBiz")
	private ISysRoleBiz sysRoleBiz;
	List<RoleAuthority> list = null;
	
	@RequestMapping("/get")
	public String get(HttpServletRequest request,RoleAuthorityVo roleAuthorityVo,Model model) throws UnsupportedEncodingException{	//获取信息
		try {
			list = roleAuthorityBiz.getRoleAuthorityByRoleAuthorityVo(roleAuthorityVo);
			model.addAttribute("list",list);
			int totalPages = roleAuthorityBiz.getTotalPages(roleAuthorityVo).getTotalPages();
			model.addAttribute("roleAuthorityVo",roleAuthorityVo);
			model.addAttribute("totalPages",totalPages);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "roleAuthority/index";
	}
	
	@RequestMapping("/delete")
	public String delete(HttpServletRequest request,@RequestParam(value="roleAuthorityId")Integer roleAuthorityId,RoleAuthorityVo roleAuthorityVo,Model model) throws UnsupportedEncodingException{	//删除
		request.setCharacterEncoding("utf-8");
		int result = roleAuthorityBiz.delete(roleAuthorityId);
		if(result > 0) {
			model.addAttribute("msg", "删除成功！");
		}else{
			model.addAttribute("msg", "删除失败！");
		}
		return "redirect:/roleAuthority/get";
	}
	
	@RequestMapping("/addPage")
	public String addPage(Model model){	//进入添加页面
		//获取所有权限集合
		List<FunctionAuthority> functionAuthorityList = functionAuthorityBiz.getFunctionAuthorityByName("");
		//获取所有角色名称集合
		List<SysRole> sysRoleList = sysRoleBiz.getAddSysRole();
		model.addAttribute("functionAuthorityList",functionAuthorityList);
		model.addAttribute("sysRoleList",sysRoleList);
		return "roleAuthority/add";
	}
	
	@RequestMapping("/add")
	public String add(HttpServletRequest request,Model model) throws UnsupportedEncodingException{	//执行添加角色信息操作
		request.setCharacterEncoding("utf-8");
		int roleId = Integer.parseInt(request.getParameter("roleId"));
		int functionAuthorityId = Integer.parseInt(request.getParameter("functionAuthorityId"));
		//int deleteFlag = Integer.parseInt(request.getParameter("deleteFlag"));
		RoleAuthority roleAuthority = new RoleAuthority();
		roleAuthority.setRoleId(roleId);
		roleAuthority.setFunctionAuthorityId(functionAuthorityId);
		//roleAuthority.setDeleteFlag(deleteFlag);
		if(roleAuthorityBiz.fetchRoleAuthority(roleId, functionAuthorityId) == null){
			int result = roleAuthorityBiz.add(roleAuthority);
			if(result > 0){
				model.addAttribute("msg", "添加成功！");
				return "redirect:/roleAuthority/get";
			}else{
				model.addAttribute("msg", "添加失败！");
				return "roleAuthority/add";
			}
		}else{
			model.addAttribute("msg", "已拥有该权限！");
			//获取所有权限集合
			List<FunctionAuthority> functionAuthorityList = functionAuthorityBiz.getFunctionAuthorityByName("");
			//获取所有角色名称集合
			List<SysRole> sysRoleList = sysRoleBiz.getAddSysRole();
			model.addAttribute("functionAuthorityList",functionAuthorityList);
			model.addAttribute("sysRoleList",sysRoleList);
			return "roleAuthority/add";
		}
	}
	
	@RequestMapping("/modifyPage")
	public String modifyPage(@RequestParam(value="roleAuthorityId")Integer roleAuthorityId,Model model){	//进入修改页面
		RoleAuthority roleAuthority = roleAuthorityBiz.findRoleAuthorityByRoleId(roleAuthorityId);
		//获取所有权限集合
		List<FunctionAuthority> functionAuthorityList = functionAuthorityBiz.getFunctionAuthorityByName("");
		model.addAttribute("functionAuthorityList",functionAuthorityList);
		model.addAttribute("roleAuthority",roleAuthority);
		return "roleAuthority/modify";
	}
	
	@RequestMapping("/update")
	public String update(HttpServletRequest request,RoleAuthorityVo roleAuthorityVo,Model model) throws UnsupportedEncodingException{	//修改权限信息
		request.setCharacterEncoding("utf-8");
		int roleAuthorityId = Integer.parseInt(request.getParameter("roleAuthorityId"));
		RoleAuthority roleAuthority = roleAuthorityBiz.findRoleAuthorityByRoleId(roleAuthorityId);
		int functionAuthorityId = Integer.parseInt(request.getParameter("functionAuthorityId"));
		int roleId = Integer.parseInt(request.getParameter("roleId"));
		System.out.println(roleId);
		if(roleAuthorityBiz.fetchRoleAuthority(roleId, functionAuthorityId) == null){
			roleAuthority.setFunctionAuthorityId(functionAuthorityId);
			int result = roleAuthorityBiz.update(roleAuthority);
			if(result > 0){
				model.addAttribute("msg", "修改成功！");
				return "redirect:/roleAuthority/get";
			}else{
				model.addAttribute("msg","修改失败！");
				return "roleAuthority/modify";
			}
		}else{
			model.addAttribute("msg", "已拥有该权限！");
			roleAuthority = roleAuthorityBiz.findRoleAuthorityByRoleId(roleAuthorityId);
			//获取所有权限集合
			List<FunctionAuthority> functionAuthorityList = functionAuthorityBiz.getFunctionAuthorityByName("");
			model.addAttribute("functionAuthorityList",functionAuthorityList);
			model.addAttribute("roleAuthority",roleAuthority);
			return "roleAuthority/modify";
		}
	}
}
